package com.unicom.projt.query;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.fee.PaymentInputPhoneNumberPage;
import com.unicom.projt.fee.PaymentPage;
import com.unicom.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-7
 * Time: 下午2:05
 */
public class QueryAvailableBalancePage extends Activity implements View.OnClickListener {
    private TextView mPhoneNumber;
    private TextView mBalanceAmount;
    private TextView mCurrentDate;
    private Handler mHandler;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 7;
    private QueryController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.query_available_balance);
        mController=new QueryController(this);
        mHandler = new Handler();
        initData();
        queryAvailableBalance();
        this.findViewById(R.id.available_balance_back_btn).setOnClickListener(this);
        this.findViewById(R.id.available_balance_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.available_balance_payment_btn).setOnClickListener(this);
    }

    private void initData() {
        mPhoneNumber = (TextView) this.findViewById(R.id.available_balance_phone_number);
        mPhoneNumber.setText(UserHolder.phoneNumber);
        mCurrentDate = (TextView) this.findViewById(R.id.available_balance_current_date);
        mCurrentDate.setText(DateUtils.formatCurrentDate());
        mBalanceAmount = (TextView) this.findViewById(R.id.available_balance_amount);
    }

    private void queryAvailableBalance() {
        showDialog(DIALOG_PROGRESS);
//        updateAvailableBalanceUI("123000");
        mController.queryAB(UserHolder.phoneNumber);
    }
    
    public void updateAvailableBalanceUI(final String value){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Double amount=Double.valueOf(value);
                amount=amount/1000;
                mBalanceAmount.setText(amount.toString());
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.available_balance_back_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.available_balance_logout_btn:
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;
            case R.id.available_balance_payment_btn:
                PageJumpUtils.jump(this, PaymentInputPhoneNumberPage.class);
                this.finish();
                break;
        }
    }
    private void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
